var pool = require('./db');
var crypto = require('crypto');

module.exports = {
    hash: function(str) {
        return crypto.createHmac('sha1', str).update('love').digest('hex');
    },
    reg: function(username, password, regtime, cb) {
        pool.getConnection(function(err, connection) {
            if(err) throw err;
            connection.query('select id from user where username=?', [username], function(err, res) {
                if(err) throw err;
                if(res.length) {
                    cb({isExisted: true}); // 自定义回调
                    connection.release();
                } else {
                    var params = {
                        username: username,
                        password: password,
                        regtime: regtime
                    };
                    connection.query('insert into user set', params, function(err, res) {
                        console.log(err, res);
                        if(err) throw err;
                        cb(res);
                        connection.release();
                    });
                }
            });
        });
    }
};